<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\CarOwner\Owner;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\CarOwner\OwnerList as OwnerList;
use AuctionCore\CarOwner\OwnerUserList as OwnerUserList;
use AuctionCore\System\AuctionException;
use AuctionCore\Common\Page;

/**
 * 卖车用户查询
 * @author Xmh
 */
class CarOwnerCheckController extends BaseController {

    public function __construct() {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        //获取父级渠道来源
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        //获取渠道来源列表
        $cols = 'a.from_id,a.from_name,b.from_name AS pname,b.from_id AS pid,b.sort AS psort';
        $condition['a.pid'] = array('neq', '-2');
        $condition['a.is_delete'] = array('eq', '0');
//        $condition['a.from_id'] = array('neq', '-1');
        $order = 'psort ASC,from_id ASC,pid DESC';
        $from = M("come_from")->alias('a')
                ->join('INNER JOIN (SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
                ->field($cols)
                ->where($condition)
                ->order($order)
                ->select();
        $this->assign('from', $from);
        $this->assign('pfrom', $pfrom);

//        $servers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(389);
        $servers = \AuctionCore\Account\Back\Employee::get_branch_emp(C('SERVER.0'));
//        $checkers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(382);
        $checkers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(C('APPRAISER.0'));
        $this->assign("server", $servers);
        $this->assign("checker", $checkers);

        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id',$branch_id);


        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = array();
        foreach($bc_city as $kb=>$vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city'=>$kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        $this->assign('comefrom_list',$comefrom_list);
        $this->assign('city_fours_list',$city_fours_list);
        $this->display();
    }

    /**
     * 卖车车主列表
     */
    public function car_owner_list() {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $params['car_model'] = I('get.car_model');
//        $params['brand_id'] = I('get.brand_id/d');
//        $params['series_id'] = I('get.series_id/d');
        $params['location_area'] = I('get.location_area');
        $params['status'] = I('get.status');
        $params['seller_name'] = I("get.seller_name");
        $params['server'] = I('get.server');
        $params['checker'] = I('get.checker');
        $params['comefrom'] = I('get.comefrom', -2);
        $params['emp_id'] = $this->emp_id;
        $params['auth'] = true;

        $params['location_area'] = I('get.location_area');
        if(!$params['location_area']){
            $params['location_area'] = !$this->is_hq?array_flip($this->bc_city):array();
        }

        //获取list
        $ol = new OwnerList();
        $result = $ol->get_carowner_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
//        dump($list);
        $show = $Page->show(); // 分页显示输出
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);

        $show_from = $this->get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    /**
     * 操作 - 查看
     */
    public function car_owner_info() {
        $p = I("get.p");
        $owner_id = I("get.id");
        $o = new Owner($owner_id);
        $owner = $o->detail();
        $addr = new \AuctionCore\Other\Address();
        $province = $addr->get_name_by_code($owner['reserve_province']);
        $city = $addr->get_name_by_code($owner['reserve_city']);
        $area = $addr->get_name_by_code($owner['reserve_area']);
        $location_area = $addr->get_name_by_code($owner['location_area']);
        $owner['location_area'] = $location_area;
        $owner['address'] = $province . " " . $city . " " . $area . " " . $owner['reserve_address'];
        $owner['owner_type_name'] = C('OWNER_TYPE_NAME')[$owner['owner_type']];
        $this->assign('owner', $owner);

        //预约地址信息
        $service_method = $owner['service_method'];
        if ($service_method == 3) {
            $reserve_store = $owner['reserve_store'];
            $reserve_address = C('STORE')[$reserve_store];
        } elseif ($service_method == 2) {
            $addr = new \AuctionCore\Other\Address();
//            $reserve_province = $addr->get_name_by_code($owner['reserve_province']);
//            $reserve_city = $addr->get_name_by_code($owner['reserve_city']);
            $reserve_area = $addr->get_full_name($owner['reserve_area']);
            $reserve_addr = $owner['reserve_address'];
            $reserve_address = $reserve_area . $reserve_addr;
        } else {
            $reserve_address = '未知';
        }

        $this->assign("reserve_address", $reserve_address);
        //提交的信息
        $sub_info = array(
            'createtime' => $owner['posttime'],
            'status' => '提交信息',
            'man' => $owner['seller_name']
        );

        //获取跟踪信息
        $status = $owner['business_status'];
        $reserve_list = array();
        if ($status == 2 || $status == 3) {
            $condition['type'] = $status;
            $condition['owner_id'] = $owner_id;
            $reserve = $o->get_reserve_single_log($condition);
            if ($reserve) {
                foreach ($reserve as $r) {
                    $reserve_list = $r;
                }
            } else {
                $reserve_list['type'] = 2;
                $reserve_list['next_trace_time'] = $owner['next_trace_time'];
                $reserve_list['remark'] = $owner['reserve_remark'];
            }
        }
        $this->assign("reserve", $reserve_list);

        //------获取跟踪日志
        if ($owner['service_id']) {
            $reserve_log = $o->get_reserve_log();
            $reserve_log_info = array();
            foreach ($reserve_log as $kr => $vr) {
                $reserve_log_info[$kr]['createtime'] = $vr['create'];
                switch ($vr['type']) {
                    case 1:
                        $status = '跟踪中';
                        break;
                    case 2:
                        $status = '跟踪中';
                        break;
                    case 3:
                        $status = '预约失败';
                        break;
                    case 4:
                        $status = '预约成功';
                        break;
                    case 5:
                        $status = '分配成功';
                        break;
                    case 501:
                        $status = '检测成功';
                        break;
                    case 502:
                        $status = '检测失败';
                        break;
                    case 5021:
                        $status = '检测改约';
                        break;
                    case 5022:
                        $status = '检测驳回';
                        break;
                    case 5023:
                        $status = '检测流失';
                        break;
                    case 5024:
                        $status = '检测退单';
                        break;
                    case 6:
                        $status = '分配改约';
                        break;
                    case 7:
                        $status = '分配滞留';
                        break;
                }
                $man = Employee::user_info_id($vr['emp_id']);
                $reserve_log_info[$kr]['status'] = $status;
                $reserve_log_info[$kr]['createtime'] = $vr['createtime'];
                $reserve_log_info[$kr]['man'] = $man['real_name'];
            }
        }
        $this->assign('sub_info', $sub_info);
        $this->assign('reserve_log', $reserve_log_info);
        $this->assign("p", $p);
        /**
         * 根据渠道来源获取渠道信息和跟踪信息
         */
        $comefrom = $owner['comefrom'];
        $business_status = $owner['business_status'];
        $comefrom_info = array();
        if ($comefrom == '0' || $comefrom == '1') { //第三方平台 跟踪信息，提交信息是平台发布时间posttime，渠道信息提交时间为数据添加时间posttime
            $comefrom_info['type'] = '1';
            $comefrom_info['publish_time'] = $owner['posttime'];
            $comefrom_info['posttime'] = $owner['posttime'];
            $comefrom_info['status'] = '提交信息';
            if ($business_status == '1') {
                $comefrom_info['man'] = '';
            } else {
                $employee = Employee::user_info_id($owner['service_id']);
                $comefrom_info['man'] = $employee['real_name'];
            }
            //历史数据自己，有客服显示操作为客服，无客服则显示评估师
        } elseif ($comefrom == '-1') {
            $comefrom_info['type'] = '3';
            $comefrom_info['publish_time'] = $owner['posttime'];
            $comefrom_info['posttime'] = $owner['posttime'];
            $comefrom_info['status'] = '提交信息';
            $employee = Employee::user_info_id($owner['service_id']);
            $checkerman = Employee::user_info_id($owner['checker_id']);
            if ($employee) {
                $comefrom_info['man'] = $employee['real_name'];
            } else {
                $comefrom_info['man'] = $checkerman['real_name'];
            }
            $comefrom_info['man'] = $employee['real_name'];
            //线上M站、WEB站、微信站、APP端，渠道提交与跟踪发生时间相同posttime，操作人为客服
        } elseif ($comefrom == '7' || $comefrom == '8' || $comefrom == '9' || $comefrom == '10') {
            $comefrom_info['type'] = '3';
            $comefrom_info['publish_time'] = $owner['posttime'];
            $comefrom_info['posttime'] = $owner['posttime'];
            $comefrom_info['status'] = '提交信息';
            $employee = Employee::user_info_id($owner['service_id']);
            if ($business_status == '1') {
                $comefrom_info['man'] = "";
            } else {
                $comefrom_info['man'] = $employee['real_name'];
            }
        } elseif ($comefrom == '11' || $comefrom == '12' || $comefrom == '13') { //400电话 跟踪信息是客服新增数据的时间和操作人是客服
            $comefrom_info['type'] = '4';
            $comefrom_info['publish_time'] = $owner['posttime'];
            $comefrom_info['posttime'] = $owner['posttime'];
            $comefrom_info['status'] = '提交信息';
            $employee = Employee::user_info_id($owner['service_id']);
            $comefrom_info['man'] = $employee['real_name'];
        } else {//4S店门店 只能是预约成功，操作人为评估师
            $comefrom_info['type'] = '5';
            $comefrom_info['publish_time'] = $owner['posttime'];
            $comefrom_info['posttime'] = $owner['posttime'];
            $comefrom_info['status'] = '提交信息';
            $checkman = Employee::user_info_id($owner['checker_id']);
            $comefrom_info['man'] = $checkman['real_name'];
        }
        $this->assign("comefrom_info", $comefrom_info);
        $show_from = $this->get_from();  //获取渠道来源信息  
        $this->assign('show_from', $show_from);
        $this->display();
    }

    //获取渠道来源信息
    private function get_from() {
        $from = M("come_from a")->field("a.from_id,a.from_name,b.from_name AS pname,b.from_id AS pid,b.sort AS psort")
                ->join('(SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
                ->where("a.pid!=-2 AND a.is_delete=0")
                ->order("psort ASC,pid DESC")
                ->select();
        $show_from = array();
        foreach ($from as $key => $val) {
            $show_from[$val['from_id']] = $val['pname'] . '-' . $val['from_name'];
        }
        return $show_from;
    }

    //获取所有客服
    public function getServers() {
        $servers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(389);
        if (empty($servers)) {
            $this->helpJsonResult(500, 'empty');
        }
        $this->helpJsonResult(200, 'server list', $servers);
    }

    //获取评估师
    public function getCheckers() {
        $checkers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(382);
        if (empty($checkers)) {
            $this->helpJsonResult(500, 'empty');
        }
        $this->helpJsonResult(200, 'checker list', $checkers);
    }

}
